package com.meepo.usercenter.center

import android.os.Bundle
import android.view.View
import com.meepo.basic.AppCoreModule
import com.meepo.basic.components.view.MeepoFragment
import com.meepo.basic.router.RouterManager
import com.meepo.framework.design.helper.ImageLoader
import com.meepo.sdk.utils.ActivityHelper
import com.meepo.basic.usersettings.SettingsActivity
import com.meepo.usercenter.R
import com.meepo.basic.schema.user.User
import com.meepo.usercenter.API
import com.meepo.usercenter.feedback.FeedbackActivity
import kotlinx.android.synthetic.main.user_center_fragment.*

/**
 * @author
 *
 * @date  2020/10/29 8:29 AM
 * @version 1.0
 */
class UserCenterFragment : MeepoFragment() {


    override fun refreshOnVisible(): Boolean = true

    private fun applyUserSummary(user: User?) {

        if (user != null) {
            login_info_ll.visibility = View.GONE
            user_info_ll.visibility = View.VISIBLE


//            ImageLoader.loadImageBackground(this.requireContext(), user.getCover(), cover_rl)

            ImageLoader.loadImage(requireContext(), user.getCover(), cover_iv)

            ImageLoader.loadImage(this.requireContext(), user.avatar ?: "", avatar_ci)
            username_tv.text = user.nickname ?: ""
            desc_tv.text = user.sign ?: ""

            avatar_ci.setOnClickListener {
                user.id?.let {
//                    ActivityHelper.launch(this.requireActivity(), com.meepo.usercenter.profile.UserProfileActivity::class.java, "userId", it)

                    RouterManager.UserModule.routeToUserProfile(requireContext(), it)
                }
            }
        } else {
            login_info_ll.visibility = View.VISIBLE
            user_info_ll.visibility = View.GONE
        }
    }

    override fun initData() {

        val userSummary = AppCoreModule.getUserSummary()

        if (userSummary != null) {

            userSummary.id?.let {
                presenter.call(
                    API.instance().getUserInfo(it),
                    { rsp -> applyUserSummary(rsp.data) },
                    { applyUserSummary(null) }
                )
            }
        } else {
            applyUserSummary(null)
        }
    }

    override fun initView(view: View, savedInstanceState: Bundle?) {

//        StatusBarHelper.setMargin(this.requireContext(), tool_bar)

//        StatusBarUtil.setTransparentForImageViewInFragment(requireActivity(), tool_bar)

        login_btn.setOnClickListener { AppCoreModule.login(requireActivity()) }

        settings_iv.setOnClickListener {
            ActivityHelper.launch(this.requireActivity(), SettingsActivity::class.java)
        }

        publish_it.setOnClickListener {
            RouterManager.FeedModule.routeToHistory(requireContext())
        }

        comment_it.setOnClickListener {
            RouterManager.FeedModule.routeToHistory(requireContext(), index = 1)
        }

        collect_it.setOnClickListener {
            RouterManager.FeedModule.routeToHistory(requireContext(), index = 2)
        }

        like_it.setOnClickListener {
            RouterManager.FeedModule.routeToHistory(requireContext(), index = 3)
        }

        history_it.setOnClickListener {
            RouterManager.FeedModule.routeToHistory(requireContext(), index = 4)
        }


        feedback_it.setOnClickListener {
            ActivityHelper.launch(this.requireActivity(), FeedbackActivity::class.java)
        }


    }

    override fun setUpContentLayout(): Int = R.layout.user_center_fragment

    companion object {
        private var userCenterFragment: UserCenterFragment? = null

        fun instance(): UserCenterFragment {

            if (userCenterFragment == null) {
                userCenterFragment = UserCenterFragment()
            }

            return userCenterFragment!!
        }
    }
}